<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
namespace App\Controller;

use App\Model\Order;
use App\Model\User;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\Redis\Redis;
use Hyperf\Utils\ApplicationContext;



/**
 * @Controller()
 */
class UserCacheController extends AbstractController
{

    private $orderModel;

    public function __construct(Order $order)
    {
        $this->orderModel = $order;
    }

    /**
     * @return array
     * @RequestMapping(path="getList", methods="get,post")
     *
     */
    public function getList(): array
    {
        $users = User::findManyFromCache(['1,2,3'])->toArray();
        return $users;
    }

    /**
     * @return array
     * @RequestMapping(path="getOne", methods="get,post")
     *
     */
    public function getOne(): array
    {
        $users = User::findFromCache(1)->toArray();
        return $users;
    }

    /**
     * @return array
     * @RequestMapping(path="getTwo", methods="get,post")
     *
     */
    public function getTwo(): array
    {
        $redis = ApplicationContext::getContainer()->get(Redis::class);
        $redis->set("test","guoliang");
        $res = $redis->get("test");

        return [
            'res'=>$res
        ];
    }

    /**
     * @param RequestInterface $request
     * @return array
     * @RequestMapping(path="getThree", methods="get,post")
     */
    public function getThree(RequestInterface $request): array
    {

        $res = $this->orderModel->getThree((int)$request->query("id"));
        return [
            'res'=>$res
        ];
    }


}
